package common

import (
	"context"
	"encoding/json"
	contract2 "git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/repository/common"
	"git.myscrm.cn/golang/stark/v4"
)

type FormService struct {
	formRepository contract2.FormRepositoryIface
}

func NewFormService(formRepository contract2.FormRepositoryIface) FormServiceIface {
	return &FormService{
		formRepository: formRepository,
	}
}

func (f *FormService) GetFormTemplateConfig(ctx context.Context, templateId string) ([]map[string]interface{}, error) {
	templateList, err := f.formRepository.GetFormTemplateConfig(ctx, templateId)
	if err != nil {
		return nil, err
	}

	// 数据格式转换，空值补充
	if templateList != nil {
		for key, temp := range templateList {
			//分组处理
			groupKey := temp["field_name"].(string)
			temp["field_component"] = "Group"
			fieldComponentProps := temp["field_component_props"].(string)
			var groupFieldPropsMap map[string]interface{}
			err := json.Unmarshal([]byte(fieldComponentProps), &groupFieldPropsMap)
			if err != nil {
				stark.Logger.Errorf(ctx, "模板配置json解析失败，key:%s,value:%s", groupKey, fieldComponentProps)
				return nil, err
			}
			temp["field_component_props"] = groupFieldPropsMap
			if _, ok := temp["sort"]; !ok {
				temp["sort"] = 0
			}
			//if _, ok := temp["field_name"]; !ok {
			//	temp["field_name"] = ""
			//}
			delete(temp, "field_name") //前端要求删除group field_name
			//filed 字段处理
			if _, ok := temp["children"]; ok {
				childrenList := make([]map[string]interface{}, len(temp["children"].([]interface{})))
				for i, item := range temp["children"].([]interface{}) {
					if itemMap, ok := item.(map[string]interface{}); ok {
						childrenList[i] = itemMap
					} else {
						stark.Logger.Errorf(ctx, "模板配置分组children格式错误，解析失败，key:%s,value:%s", groupKey, temp["children"])
					}
				}
				for k, children := range childrenList {
					childKey := children["field_name"].(string)
					childFieldComponentProps := children["field_component_props"].(string)
					var childFieldPropsMap map[string]interface{}
					err := json.Unmarshal([]byte(childFieldComponentProps), &childFieldPropsMap)
					if err != nil {
						stark.Logger.Error(ctx, "模板配置json解析失败，gropy_key:%s,child_key:%s,value:%s", groupKey, childKey, childFieldComponentProps)
						return nil, err
					}
					children["field_component_props"] = childFieldPropsMap
					if _, ok := children["sort"]; !ok {
						children["sort"] = 0
					}
					if _, ok := children["is_default"]; !ok {
						children["is_default"] = 0
					}
					if _, ok := children["required"]; !ok {
						children["required"] = 0
					}
					if _, ok := children["field_component"]; !ok {
						children["field_component"] = ""
					}
					if _, ok := children["field_name"]; !ok {
						children["field_name"] = ""
					}
					childrenList[k] = children
				}

				temp["children"] = childrenList
			}
			templateList[key] = temp
		}
	}

	return templateList, nil
}

func (f *FormService) GetFormDataRule(ctx context.Context, key string) (map[string]interface{}, error) {
	ruleList, err := f.formRepository.GetFormDataRule(ctx, key)
	if err != nil {
		return nil, err
	}
	result := make(map[string]interface{}, 0)
	if ruleList != nil {
		for _, item := range ruleList {
			ruleKey := item["rule_key"].(string)
			ruleValueStr := item["rule_value"].(string)

			// 解析ruleValue字段的值为map[string]interface{}
			var ruleValueMap map[string]interface{}
			err := json.Unmarshal([]byte(ruleValueStr), &ruleValueMap)
			if err != nil {
				stark.Logger.Error(ctx, "数据规则json解析失败，key:%s,value:%s", ruleKey, ruleValueStr)
				return nil, err
			}
			item["rule_value"] = ruleValueMap
			result[ruleKey] = ruleValueMap
			//ruleList[key] = item
		}
	}
	return result, nil
}
